<template>
  <van-pull-refresh v-model="loading" @refresh="onRefresh">
    <div class="section user-section">
      <div class="head">
        <van-nav-bar class="nav-bar">
          <template #left>
            <van-icon name="arrow-left" @click="router.go(-1)" />
          </template>

          <template #title>
            <div class="title">我的报告</div>
          </template>
        </van-nav-bar>
      </div>
      <van-search
        v-model="value"
        show-action
        placeholder="请输入要搜索的报告"
        @search="onSearch"
      >
        <template #action>
          <div @click="newReport" class="newReport">新建</div>
        </template>
      </van-search>
      <div
        class="reportCard"
        v-for="report in formState.reportList"
        :key="report.id"
        @click="router.push({name:'editReport', params:{fromWhere:'oldRp', reportId:report.id}})"
      >
        <van-card v-if="report.isShow">
          <template #title>
            <div class="title">
              {{ report.reportTime }}，{{ report.reportType }}
            </div>
          </template>
          <template #desc>
            <div class="desc">批改人:{{ report.devPeo }}</div>
          </template>
          <template #tags>
            <div class="tags">
              <van-tag size="medium" :type="report.type">{{
                report.status
              }}</van-tag>
            </div>
          </template>
          <template #footer>
            <van-button
              type="danger"
              size="mini"
              @click.stop="deleteReport(report.id)"
              >删除报告</van-button
            >
          </template>
        </van-card>
      </div>
    </div>
  </van-pull-refresh>
</template>

<script setup>
import { ref, reactive, computed, onMounted, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { Dialog, Toast } from "vant";
import toasts from "/@/views/components/toasts";
import YuCard from "/@/components/YuCard";
import { useStore } from "vuex";
import { getReportList, deleteRp } from "/@/api/report";
const loading = ref(false);

const router = useRouter();
const value = ref("");
const store = useStore();
const onSearch = (val) => {
  showWhich(val)
}
const token = store.state.storage.user.token;

watch(value,(newValue, oldValue)=>{
  showWhich(newValue)
})

function showWhich(val){
  let report = formState.reportList
  for (let i = 0; i < report.length; i++) {
      let a = val
      let b = report[i].reportTime //日期
      let c = report[i].reportType //类型
      if ((b.search(a) != -1) || (c.search(a) != -1)) {
        report[i].isShow = true
      } else {
        report[i].isShow = false
      }
    }
}

const newReport = () => {
  router.push({ name: "editReport", params: { fromWhere: "newRp" } });
};
const formState = reactive({
  reportList: [],
});

onMounted(() => {
  doGetReport();
});

const onRefresh = () => {
  doGetReport();
};

const deleteReport = (reportId) => {
  Dialog.confirm({
    title: "确认",
    message: "确定删除报告么",
  })
    .then(() => {
      deleteRp(
        {
          token,
          reportId,
        },
        (res) => {
          if (res.code === 0) {
            toasts.success("删除成功");
            doGetReport();
          } else {
            toasts.fail("删除失败" + res.msg);
          }
        }
      );
    })
    .catch(() => {});
};

// 获取报告
function doGetReport() {
  getReportList(
    {
      token,
    },
    (res) => {
      if (res.code === 0) {
        console.log(res);
        let reportList = res.reportlist;
        for (let i = 0; i < reportList.length; i++) {
          reportList[i]["type"] = "warning";
          if (reportList[i].isCorrect && !reportList[i].isDone) {
            reportList[i]["type"] = "primary";
          } else if (reportList[i].isDone) {
            reportList[i]["type"] = "success";
          }
          if (reportList[i]["type"] === "warning") {
            reportList[i]["status"] = "未批改";
          } else if (reportList[i]["type"] === "primary") {
            reportList[i]["status"] = "正在批改";
          } else if (reportList[i]["type"] === "success") {
            reportList[i]["status"] = "可以抄写";
          }
        }
        formState.reportList = reportList;
        loading.value = false;
      }
    }
  );
}
</script>


<style lang="scss" scoped>
.user-section {
  background: var(--yu-gray-color--light);
  min-height: 100vh;
  .reportCard {
    margin: 3px;
    border-radius: 3px;
    padding: 5px;
    .title {
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 7px;
      font-size: large;
    }
    .desc {
      display: flex;
      align-items: center;
      margin: 7px;
      justify-content: center;
      font-size: small;
    }
    .tags {
      display: flex;
      margin: 7px;
      align-items: center;
      justify-content: center;
    }
  }
  .newReport {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #61cd7f;
    margin: 1px;
    padding: 1px;
    border-radius: 3px;
    color: white;
    width: 1rem;
  }
  .head {
    height: 50px;
    position: relative;
    .nav-bar {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      background: var(--yu-linear-bg-color);
      border: none;
      .van-icon {
        color: var(--yu-white-color);
      }
      .title {
        color: var(--yu-white-color);
      }
      &:after {
        border: none;
      }
    }
    .head-bg {
      height: 150px;
      background: var(--yu-linear-bg-color);
      position: relative;
      overflow: hidden;
      width: 100%;
      img {
        display: inline-block;
        position: absolute;
        width: 80px;
        height: auto;
        opacity: 0.2;
        &.img1 {
          top: 0;
          right: 0;
        }
        &.img2 {
          bottom: 0;
          left: 0;
        }
      }
    }
  }

  .user-info {
    position: absolute;
    top: 66px;
    left: 13px;
    width: calc(100% - 26px);
    z-index: 9;

    .card-body-wrapper {
      .avatar {
        width: 60px;
        height: 60px;
        overflow: hidden;
        border-radius: 50%;
        margin-right: 16px;
        box-shadow: var(--yu-border-shadow--light);
        img {
          width: 100%;
          height: 100%;
        }
      }
      .info {
        color: rgba(36, 45, 61, 1);
        font-size: 12px;
        line-height: 18px;
        padding-top: 10px;
        text-align: left;
        width: calc(100% - 60px - 16px);
        .user-name {
          margin-bottom: 4px;
          font-weight: 600;
        }

      }
    }

  }

  .body {
    padding: 0 13px;
    margin-top: 10px;
  }
  .user-info-card {
    display: block;
    width: 100%;
    padding: 0;
    margin-bottom: 8px;
  }
  .user-info-list {
    font-size: 15px;
  }
}
</style>